#include "head.h"
extern u32_t player_flag;

int winner(u32_t player_chess)
{
	if(player_chess)
	{
		printf("White Is The Winner\n");
		return player_chess;
	}
	printf("Black Is The Winner\n");
		
	return player_chess;
}

int judgewin(int x, int y)
{
	int j,k,n1,n2 ; 
	n1=0 ; 
	n2=0 ; 
		/*水平向左数*/ 
		for(j = x, k = y; k >= 0; k--) 
		{ 
			if(save[j][k].chess == player_flag && save[j][k].is_set) 
				n1++; 
			else 
				break ; 
		}		 

		/*水平向右数*/ 
		for(j = x, k = y; k <= 16; k++) 
		{ 
			if(save[j][k].chess == player_flag && save[j][k].is_set) 
				n2++; 
			else 
				break ; 
		} 

		if(n1 + n2 - 1 >= 5) 
		{
			winner(player_flag);
			printf("n1 = %d, n2 = %d\n", n1, n2);
			return TRUE; 
		}

		/*垂直向上数*/ 
		n1=0 ; 
		n2=0 ; 
		for(j = x, k = y ; j >= 0; j--) 
		{ 
			printf("is_set:%d\n", save[j][k].is_set);
			if(save[j][k].chess == player_flag && save[j][k].is_set) 
				n1++; 
			else 
				break ; 
		} 
		/*垂直向下数*/ 
		for(j = x, k = y; j <= 16; j++) 
		{ 
			if(save[j][k].chess == player_flag && save[j][k].is_set) 
				n2++; 
			else 
				break ; 
		} 
		if(n1 + n2 - 1 >= 5) 
		{
			winner(player_flag);
			return TRUE; 
		} 

		/*向左上方数*/ 
		n1=0 ; 
		n2=0 ; 
		for(j = x, k = y; j >= 0, k >= 0; j--, k--) 
		{ 
			if(save[j][k].chess == player_flag && save[j][k].is_set) 
				n1++; 
			else 
				break ; 
		} 
		/*向右下方数*/	 
		for(j = x, k = y; j <= 16, k <= 16; j++, k++) 
		{ 
			if(save[j][k].chess == player_flag && save[j][k].is_set) 
				n2++; 
			else 
				break ; 
		} 
		if(n1 + n2 - 1 >= 5) 
		{
			winner(player_flag);
			return TRUE; 
		}
		/*向右上方数*/ 
		n1=0 ; 
		n2=0 ; 
		for(j = x, k = y ; j >= 0 ,k <= 16; j--,k++) 
		{ 
			if(save[j][k].chess == player_flag && save[j][k].is_set) 
				n1++; 
			else 
				break ; 
		} 
		/*向左下方数*/ 
		for(j = x, k = y; j <= 16, k >= 0; j++, k--) 
		{ 
			if(save[j][k].chess == player_flag && save[j][k].is_set) 
				n2++; 
			else 
				break ; 
		} 
		if(n1 + n2 - 1 >= 5) 
		{
			winner(player_flag);
			return TRUE; 
		}
		
		return FALSE; 
}


